//
//  RankCell.m
//  Kugou-OC
//
//  Created by BH on 16/6/7.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "RankCell.h"
#import "GlobalDefine.h"
#import "TapImageView.h"
#import "RankModel.h"
#import <SDAutoLayout/UIView+SDAutoLayout.h>
#import <SDWebImage/UIImageView+WebCache.h>

@interface RankCell ()

@property(nonatomic,strong)UILabel *rank1;
@property(nonatomic,strong)UILabel *rank2;
@property(nonatomic,strong)UILabel *rank3;

@end

@implementation RankCell

+(instancetype)cellWithTableView:(UITableView *)tableView{
//    static NSString *identifier = @"RankCell";
//    RankCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (cell == nil) {
//        cell = [[RankCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//    }else{
//        for (UIView *subview in cell.contentView.subviews) {
//            [subview removeFromSuperview];
//        }
//    }
    RankCell *cell = [[RankCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    return cell;
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.separatorInset = UIEdgeInsetsMake(0, 100, 0, 0);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _iconImgV = ALLOC(TapImageView);
        _titleLab = ALLOC(UILabel);
        _firstLab = ALLOC(UILabel);
        _secondLab = ALLOC(UILabel);
        _thirdLab = ALLOC(UILabel);
        _rank1 = ALLOC(UILabel);
        _rank2 = ALLOC(UILabel);
        _rank3 = ALLOC(UILabel);
        
        [self.contentView sd_addSubviews:@[_iconImgV,_titleLab,_firstLab,_secondLab,_thirdLab,_rank1,_rank2,_rank3]];
        
    }
    
    return self;
}

-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
}

-(void)setRankM:(RankModel *)rankM{
    _rankM = rankM;
    
    [_iconImgV sd_setImageWithURL:[NSURL URLWithString:_rankM.imgUrl] placeholderImage:IMAGE(@"logo_blue")];
    _titleLab.text = _rankM.rankName;
    
    if (_indexPath.row == 0) {
        _rank1.text = [_rankM.children[0][@"rankname"] substringFromIndex:3];
        _firstLab.text = [_rankM.children[1][@"rankname"] substringFromIndex:3];
        _rank2.text = [_rankM.children[2][@"rankname"] substringFromIndex:3];
        _secondLab.text = [_rankM.children[3][@"rankname"] substringFromIndex:3];
        _rank3.text = [_rankM.children[4][@"rankname"] substringFromIndex:3];
        _thirdLab.text = [_rankM.children[5][@"rankname"] substringFromIndex:3];
    }else{
        _firstLab.text = _rankM.songInfo[0][@"songname"];
        _secondLab.text = _rankM.songInfo[1][@"songname"];
        _thirdLab.text = _rankM.songInfo[2][@"songname"];
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _iconImgV.sd_layout.leftSpaceToView(self.contentView,15).topSpaceToView(self.contentView,8).bottomSpaceToView(self.contentView,8).widthEqualToHeight();
    
    _titleLab.sd_layout.leftSpaceToView(_iconImgV,15).topSpaceToView(self.contentView,10).rightSpaceToView(self.contentView,0).autoHeightRatio(0);
    _titleLab.font = TITLE_SECOND_FONT;
    
    _firstLab.font = TITLE_FOUR_FONT;
    _firstLab.textColor = TITLE_FOUR_TEXT_COLOR;
    
    _secondLab.font = TITLE_FOUR_FONT;
    _secondLab.textColor = TITLE_FOUR_TEXT_COLOR;
    
    _thirdLab.font = TITLE_FOUR_FONT;
    _thirdLab.textColor = TITLE_FOUR_TEXT_COLOR;
    
    if (_indexPath.row == 0) {
        _rank1.font = TITLE_FOUR_FONT;
        _rank1.textColor = TITLE_FOUR_TEXT_COLOR;
        _rank2.font = TITLE_FOUR_FONT;
        _rank2.textColor = TITLE_FOUR_TEXT_COLOR;
        _rank3.font = TITLE_FOUR_FONT;
        _rank3.textColor = TITLE_FOUR_TEXT_COLOR;
        
        _rank1.sd_layout.leftEqualToView(_titleLab).topSpaceToView(_titleLab,5).autoHeightRatio(0).widthIs(100);
        _rank2.sd_layout.leftEqualToView(_rank1).topSpaceToView(_rank1,5).rightEqualToView(_rank1).autoHeightRatio(0);
        _rank3.sd_layout.leftEqualToView(_rank2).topSpaceToView(_rank2,5).rightEqualToView(_rank2).autoHeightRatio(0);
    }else{
        UILabel *rank1 = ALLOC(UILabel);
        rank1.backgroundColor = [UIColor redColor];
        rank1.sd_cornerRadiusFromWidthRatio = @(0.5);
        rank1.textColor = [UIColor whiteColor];
        rank1.font = TITLE_FOUR_FONT;
        rank1.textAlignment = NSTextAlignmentCenter;
        rank1.text = @"1";
        _rank1 = rank1;
        
        UILabel *rank2 = ALLOC(UILabel);
        rank2.backgroundColor = [UIColor orangeColor];
        rank2.sd_cornerRadiusFromWidthRatio = @(0.5);
        rank2.textColor = [UIColor whiteColor];
        rank2.font = TITLE_FOUR_FONT;
        rank2.textAlignment = NSTextAlignmentCenter;
        rank2.text = @"2";
        _rank2 = rank2;
        
        UILabel *rank3 = ALLOC(UILabel);
        rank3.backgroundColor = COLOR(240, 163, 46, 1);
        rank3.sd_cornerRadiusFromWidthRatio = @(0.5);
        rank3.textColor = [UIColor whiteColor];
        rank3.font = TITLE_FOUR_FONT;
        rank3.textAlignment = NSTextAlignmentCenter;
        rank3.text = @"3";
        _rank3 = rank3;
        
        [self.contentView sd_addSubviews:@[_rank1,_rank2,_rank3]];
        
        _rank1.sd_layout.leftEqualToView(_titleLab).topSpaceToView(_titleLab,5).widthIs(20).autoHeightRatio(0);
        _rank2.sd_layout.leftEqualToView(_rank1).topSpaceToView(_rank1,5).widthIs(_rank1.width).autoHeightRatio(0);
        _rank3.sd_layout.leftEqualToView(_rank2).topSpaceToView(_rank2,5).widthIs(_rank2.width).autoHeightRatio(0);
    }
    
    _firstLab.sd_layout.leftSpaceToView(_rank1,3).topEqualToView(_rank1).rightEqualToView(_titleLab).bottomEqualToView(_rank1);
    _secondLab.sd_layout.leftEqualToView(_firstLab).topEqualToView(_rank2).rightEqualToView(_firstLab).bottomEqualToView(_rank2);
    _thirdLab.sd_layout.leftEqualToView(_secondLab).topEqualToView(_rank3).rightEqualToView(_secondLab).bottomEqualToView(_rank3);
}

@end
